﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace TimeQuest.Base
{
    public class Player : GameObject
    {
        #region Constructors

        public Player(GameWorld gw)
        {
            BaseHeight = 20;
            BaseWidth = 50;
            BaseYoffset = 120;
            BaseXoffset = 5;
            GameWorld = gw;
            Speed = 5;
            InventoryItems = new List<Item>();
        }

        #endregion

        #region Properties

        public bool Moving { get; set; }
        public int Speed { get; set; }
        public GameWorld GameWorld { get; set; }
        public Directions Facing { get; set; }
        public Point WalkingTo { get; set; }
        public IList<Item> InventoryItems { get; set; }

        #endregion

        #region Public Methods

        public void KeyPress(IList<Keys> PressedKeys)
        {
            if (PressedKeys.Contains(Keys.A) ||
                PressedKeys.Contains(Keys.Left))
            {
                if (Facing == Directions.Left
                    || !Moving)
                    Moving = !Moving;
                Facing = Directions.Left;
                WalkingTo = Point.Zero;
            }
            if (PressedKeys.Contains(Keys.D) ||
                PressedKeys.Contains(Keys.Right))
            {
                if (Facing == Directions.Right
                    || !Moving)
                    Moving = !Moving;
                Facing = Directions.Right;
                WalkingTo = Point.Zero;
            }
            if (PressedKeys.Contains(Keys.W) ||
                PressedKeys.Contains(Keys.Up))
            {
                if (Facing == Directions.Up
                    || !Moving)
                    Moving = !Moving;
                Facing = Directions.Up;
                WalkingTo = Point.Zero;
            }
            if (PressedKeys.Contains(Keys.S) ||
                PressedKeys.Contains(Keys.Down))
            {
                if (Facing == Directions.Down
                    || !Moving)
                    Moving = !Moving;
                Facing = Directions.Down;
                WalkingTo = Point.Zero;
            }
        }
        public void Update(Screen s, GameTime gt)
        {
            if (Moving)
            {
                Point oldLoc = Location;
                if (WalkingTo != Point.Zero)
                {
                    if (CenterOfBaseY > WalkingTo.Y)
                        Location.Y--;
                    if (CenterOfBaseY < WalkingTo.Y)
                        Location.Y++;
                    if (CenterOfBaseX > WalkingTo.X)
                        Location.X--;
                    if (CenterOfBaseX < WalkingTo.X)
                        Location.X++;
                }
                else
                {
                    switch (Facing)
                    {
                        case Directions.Up:
                            Location.Y -= Speed;
                            break;
                        case Directions.Down:
                            Location.Y += Speed;
                            break;
                        case Directions.Left:
                            Location.X -= Speed;
                            break;
                        case Directions.Right:
                            Location.X += Speed;
                            break;
                        default:
                            break;
                    }
                }
                var collided = s.GameObjects.Where(x => x.BlocksPlayer && this.BaseRectangle.Overlapping(x.BaseRectangle));
                if (collided.Count() > 0 ||
                    (GameWorld.CurrentScreen.LeftScreen == null && CenterOfBaseX < (this.BaseWidth / 2)) ||
                    (GameWorld.CurrentScreen.RightScreen == null && CenterOfBaseX > 640 - (this.BaseWidth / 2)) ||
                    (GameWorld.CurrentScreen.TopScreen == null && CenterOfBaseY < this.BaseHeight) ||
                    (GameWorld.CurrentScreen.BottomScreen == null && CenterOfBaseY > 480 - (this.BaseHeight / 2))
                    )
                {
                    Location = oldLoc;
                    Moving = false;
                    WalkingTo = Point.Zero;
                }
                if (CenterOfBaseX < 0)
                {
                    if (GameWorld.CurrentScreen.LeftScreen != null)
                    {
                        GameWorld.CurrentScreen = GameWorld.CurrentScreen.LeftScreen;
                        Location.X = 600;
                    }
                }
                if (CenterOfBaseX > 640)
                {
                    if (GameWorld.CurrentScreen.RightScreen != null)
                    {
                        GameWorld.CurrentScreen = GameWorld.CurrentScreen.RightScreen;
                        Location.X = -10;
                    }
                }
                if (CenterOfBaseY < 0)
                {
                    if (GameWorld.CurrentScreen.TopScreen != null)
                    {
                        GameWorld.CurrentScreen = GameWorld.CurrentScreen.TopScreen;
                        Location.Y = 350;
                    }
                }
                if (CenterOfBaseY > 480)
                {
                    if (GameWorld.CurrentScreen.BottomScreen != null)
                    {
                        GameWorld.CurrentScreen = GameWorld.CurrentScreen.BottomScreen;
                        Location.Y = -50;
                    }
                }

            }
            foreach (GameObject go in s.GameObjects)
            {
                go.Update(gt);
            }
        }
        public void FillInventory(XNAFormsLib.XNAForm form)
        {
            int i = 0;
            foreach (Item itm in InventoryItems)
            {
                InventoryItemButton b = new InventoryItemButton(form.Owner);
                b.Item = itm;
                b.Location = new Point(i * 50, 0);
                form.Controls.Add(b);
                i++;
            }
        }

        #endregion
    }
}